package com.tangula.web.kashine.business;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.tangula.core.Logging;
import com.tangula.web.form.DefaultPagingBody;
import com.tangula.web.kashine.entity.ClassEntity;
import com.tangula.web.kashine.entity.CostListForm;
import com.tangula.web.kashine.entity.CostListForm2;
import com.tangula.web.kashine.entity.LoadTopForm;
import com.tangula.web.kashine.entity.TeacherPageableForm;
import com.tangula.web.kashine.repository.AccountEntityRepository;
import com.tangula.web.kashine.repository.ClassEntityRepository;
import com.tangula.web.kashine.repository.ClassStudentRefEntityRepository;
import com.tangula.web.kashine.repository.CostListRepository;
import com.tangula.web.kashine.repository.HistoryEntityRepository;
import com.tangula.web.kashine.repository.ProductEntityRepository;
import com.tangula.web.kashine.repository.StudentAwardRecordEntityRepository;

@Service
public class ClassBusinessImpl implements ClassBusiness, Logging {
	@Autowired
	ClassEntityRepository classRepo;
	@Autowired
	ClassStudentRefEntityRepository classStudentRepository;
	@Autowired
	HistoryEntityRepository historyRepository;
	@Autowired
	StudentAwardRecordEntityRepository awardRepository;
	@Autowired
	AccountEntityRepository accountRepository;
	@Autowired
	ProductEntityRepository productRepository;
	@Autowired
	CostListRepository costRepo;

	@Override
	public DefaultPagingBody<ClassEntity> showClass(String teacher) {

			DefaultPagingBody<ClassEntity> d = new DefaultPagingBody<>(0, 1000);

			logDebug("teacherId:"+teacher);
			Page<ClassEntity> page = classRepo.findByOwnerTeacher(teacher, PageRequest.of(0, 1000));
			
			d.fill(page);
			logDebug("classes:"+JSONObject.toJSONString(page));
			logDebug("classes:"+JSONObject.toJSONString(d));
			
			return d;
	}

	@Override
	public ClassEntity addClass(ClassEntity claz) {
		if (claz != null) {
			return classRepo.saveAndFlush(claz);
		}
		return null;

	}

	@Override
	public ClassEntity updateClassInfo(ClassEntity claz) {
		if (claz != null) {
			ClassEntity target = classRepo.findById(claz.getId()).orElse(null);
			if(target!=null) {
				target.setClassName(claz.getClassName());
				target.setClassDes(claz.getClassDes());
			return classRepo.saveAndFlush(target);
			}
		}
		return claz;
	}

	@Override
	public LoadTopForm loadTopItems(String teacherId) {
			List<String> topSpenderNames = accountRepository.findTopSpenderName(teacherId);
			List<String> topEarnerNames = accountRepository.findTopEarnerNames(teacherId);
			LoadTopForm ltf = new LoadTopForm();
			if(topSpenderNames.size()!=0&&topEarnerNames.size()!=0) {
				ltf.setTopEarnerName(topEarnerNames.get(0));
				ltf.setTopSpenderName(topSpenderNames.get(0));
				return ltf;
			}else {
				return null;
			}
		
		
	}

	@Override
	public Boolean removeClass(String classId) {
		classRepo.deleteById(classId);
		ClassEntity clas = classRepo.findById(classId).get();
		if (clas == null) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public DefaultPagingBody<CostListForm2> loadTeacherHomeCostList(TeacherPageableForm teacher) {
		if (teacher != null) { 
			List<CostListForm> lists = costRepo.loadTeacherHomeCostList(teacher.getCondition());
			List<CostListForm2> clists = new ArrayList<CostListForm2>();
			for(CostListForm list: lists) {
				CostListForm2 cost = new CostListForm2(list.getClassId(), list.getClassName(), list.getTotalCost()+"", list.getTopEarnerName(), list.getTopEarnerMoneyAmount()+"");
				clists.add(cost);
			}
			DefaultPagingBody<CostListForm2> dfpb = new DefaultPagingBody<>(teacher.getIndex(), teacher.getSize(), clists.size(), clists);
			return dfpb;
		}else {
			return null;
		}
	
	}

	@Override
	public Page<ClassEntity> loadAllClasses(String teacherId, Pageable pr) {
		Page<ClassEntity> res = null;
		res = classRepo.findByOwnerTeacher(teacherId, pr);
		return res;
	}

	@Override
	public boolean removeTeacherClass(String teacherId, String classId) {
		boolean res = false;
		if(classRepo.existsByIdAndOwnerTeacher(classId, teacherId)) {
			classRepo.deleteById(classId);
			res = true;
		}
		return res;
	}

}
